﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace LeanApp.Bll
{
    /// <summary>
    /// 累积分析业务
    /// </summary>
    public class CumulativeBll
    {
        /// <summary>
        /// 分析数据
        /// </summary>
        /// <param name="data">数据</param>
        public void AnalysisData(List<Dictionary<string, object>> data)
        {

            if (data == null || data.Count <= 0)
            {
                return;
            }
            _sourceData = data;
            foreach (Dictionary<string, object> item in data)
            {
                SetColMapper(item.Keys.ToList());
            }
            foreach (string item in _colMapper)
            {
                foreach (Dictionary<string, object> val in data)
                {
                    List<double> tmp = GetResultItem(item);
                    if (val.ContainsKey(item))
                    {
                        tmp.Add(Convert.ToDouble(val[item]));
                    }
                    else
                    {
                        tmp.Add(0);
                    }
                }
            }
        }

        /// <summary>
        /// 结果数据
        /// </summary>
        public Dictionary<string, List<double>> ResultData
        {
            get
            {
                return _resultData;
            }
        }

        /// <summary>
        /// 原始数据
        /// </summary>
        public List<Dictionary<string, object>> SourceData
        {
            get
            {
                return _sourceData;
            }
        }

        /// <summary>
        /// 获取列名映射
        /// </summary>
        /// <returns>列名映射</returns>
        public List<string> GetColMapper()
        {
            return _colMapper;
        }

        /// <summary>
        /// 获取结果子项
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns>结果子项</returns>
        private List<double> GetResultItem(string name)
        {
            List<double> result = null;
            if (_resultData.Keys.Contains(name))
            {
                result = _resultData[name];
                if (result == null)
                {
                    result = new List<double>();
                    _resultData.Add(name, result);
                }
            }
            else
            {
                result = new List<double>();
                _resultData.Add(name, result);
            }
            return result;
        }

        /// <summary>
        /// 设置列名映射
        /// </summary>
        /// <param name="names">列名集合</param>
        private void SetColMapper(List<string> names)
        {
            foreach (string item in names)
            {
                if (!_colMapper.Contains(item))
                {
                    _colMapper.Add(item);
                }
            }

        }

        /// <summary>
        /// 列名映射
        /// </summary>
        private List<string> _colMapper = new List<string>();

        /// <summary>
        /// 分析结果
        /// </summary>
        private Dictionary<string, List<double>> _resultData = new Dictionary<string, List<double>>();

        /// <summary>
        /// 基础数据
        /// </summary>
        private List<Dictionary<string, object>> _sourceData = new List<Dictionary<string, object>>();
    }
}
